import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:pwd_keeper/common/sqlite/util.dart';

class MainManager with ChangeNotifier {
  static MainManager instance = MainManager();

  bool verification = false;
  final sqliteUtil = SqliteUtil();

  Map globalInfo = {'id': 'treesflower', 'gesture_pwd': ''};

  void init() async {
    sqliteUtil.createDb(1);
    sqliteUtil.query('SELECT * FROM GlobalInfo').then((res) {
      if (res == null || res.length == 0) {
        // 初始化
        sqliteUtil.insert(
            'INSERT INTO GlobalInfo(id,gesture_pwd) VALUES("treesflower","")');
      } else {
        globalInfo = json.decode(json.encode(res[0]));
        // print(globalInfo);
        notifyListeners();
      }
    });
  }

  void updateGlobalPassword(String pwd) async {
    await sqliteUtil.update(
      'UPDATE GlobalInfo SET gesture_pwd = ? WHERE id = "treesflower"',
      [pwd],
    );
    verification = true;
    notifyListeners();
  }

  void globalPasswordVerification() {
    verification = true;
    notifyListeners();
  }
}
